import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		hasLogined: !!uni.getStorageSync('accessToken'),
		userInfo: {},
		hasNewCartItem: false,
		orderNo4invoice: ''
	},
	mutations: {
		login(state, userInfo) {
			console.log('登录授权 更新前store的值', state.hasLogined)
			state.hasLogined = true;
			console.log('登录授权 更新后store的值', state.hasLogined)
			state.userInfo = userInfo;
			// uni.setStorage({//缓存用户登陆状态
			//     key: 'userInfo',  
			//     data: userInfo  
			// }) 
			console.log('登录后用户信息', state.userInfo);
		},
		updateUser(state, userInfo){
			state.userInfo = userInfo;
		},
		logout(state) {
			state.hasLogined = false;
			state.userInfo = {};
			uni.removeStorage({
                key: 'userInfo'  
            })
			state.hasNewCartItem = true // 登录超时后，访问购物车页面重新请求数据
		},
		cartTrigger(state, isReaded = true){ // 添加购物车触发购物车列表查询接口
			if(isReaded){
				console.log('添加购物车成功，触发购物车列表请求', isReaded)
			}else{
				console.log('购物车列表请求结束， 恢复触发状态为', isReaded)
			}
			state.hasNewCartItem = isReaded
			console.log('是否需要触发', state.hasNewCartItem)
		},
		setOrderNoByApplyInvoice(state, orderNo = ''){
			console.log('申请发票设置订单号', orderNo)
			state.orderNo4invoice = orderNo
			console.log('申请发票设置订单号完毕', state.orderNo4invoice)
		},
		
	},
	actions: {
	
	}
})

export default store
